﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MadLibrary.Collections;

namespace MadLibrary.Web
{
    /// <summary>
    /// Holds and manages collection of HTML attributes.
    /// </summary>
    public class HtmlAttributes : Collection<HtmlAttribute>
    {
        /// <summary>
        /// Creates new empty HTML attribute collection.
        /// </summary>
        public HtmlAttributes()
            : base()
        {
            
        }
        /// <summary>
        /// Creates new HTML attribute collection with specified default attributes.
        /// </summary>
        /// <param name="atts">Specifies attributes which have to be added into new collection.</param>
        public HtmlAttributes(params HtmlAttribute[] atts)
            : base(atts)
        {

        }
        /// <summary>
        /// Parses specified input string into current HTML attribute collection.
        /// </summary>
        /// <param name="content">Specifies string whith has to be parsed.</param>
        public void Parse(string content)
        {
            if (content == "" || content == null)
                return;
            MatchCollection col = Regex.Matches(content, " (\\w*)=\"(.*)\"", RegexOptions.Singleline);
            foreach (Match m in col)
            {
                HtmlAttribute att = new HtmlAttribute();
                att.Name = m.Groups[1].Value;
                att.Value = m.Groups[2].Value;
                this.Add(att);
            }
        }
        /// <summary>
        /// Provides static parsing of input string.
        /// </summary>
        /// <param name="content">Specifies string whith has to be parsed into new HtmlAttribute collection.</param>
        /// <returns></returns>
        public static HtmlAttributes StaticParse(string content)
        {
            if (content == "" || content == null)
                return new HtmlAttributes();
            HtmlAttributes attcol = new HtmlAttributes();
            MatchCollection col = Regex.Matches(content, " (\\w*)=\"(.*)\"", RegexOptions.Singleline);
            foreach (Match m in col)
            {
                HtmlAttribute att = new HtmlAttribute();
                att.Name = m.Groups[1].Value;
                att.Value = m.Groups[2].Value;
                attcol.Add(att);
            }
            return attcol;
        }
    }
}
